package com.ymatou.query.trading.api.service;

import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ymt.base.YmatouBaseCall;
import com.ymttest.common.entitie.CouponInfo;
import com.ymttest.utils.EnvSetup;

/**
 * 获取优惠券信息Call FreeMaker Template自动生成代码
 */
public class GetCouponInfoCall extends YmatouBaseCall {
	private static String opurl = "api/App/GetCouponInfo";

	public GetCouponInfoCall() {
		super(EnvSetup.getSystemUrl("api_order_query"), opurl, "GET", "PARAM");
	}

	@Override
	public void addDefHeader() {
		client.addHeader("Accept", "text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}

	/**
	 * 获取Code
	 * 
	 * @return
	 */
	public String getCode() {
		String Code = null;
		try {
			Code = new JSONObject(getReturnData()).getString("Code");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Code;
	}

	/**
	 * 获取Msg
	 * 
	 * @return
	 */
	public String getMsg() {
		String BCode = null;
		try {
			BCode = new JSONObject(getReturnData()).getString("Msg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BCode;
	}

	/**
	 * 获取Data
	 * 
	 * @return
	 */
	public JSONObject getData() {
		JSONObject Data = null;
		try {
			Data = new JSONObject(getReturnData()).getJSONObject("Data");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Data;
	}

	/**
	 * 获取CouponInfo信息
	 * 
	 * @return
	 */
	public CouponInfo getCouponInfo() {
		// ArrayList<CouponInfo> list = new ArrayList<CouponInfo>();
		// JSONArray temp = null;
		// try {
		// JSONObject temp2 = getData().getJSONObject("CouponInfo");
		// CouponInfo io = (CouponInfo) FormatUtil.GSONToObject(
		// temp2.toString(), CouponInfo.class);
		// list.add(io);
		//
		// } catch (JSONException e) {
		// e.printStackTrace();
		// }
		// return list;
		CouponInfo cinfo = new CouponInfo();
		try {
			String rd = getData().getString("CouponInfo");
			Gson gson = new Gson();
			cinfo = gson.fromJson(rd, new TypeToken<CouponInfo>() {
			}.getType());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cinfo;
	}
}
